<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="名称">
                <a-input allow-clear placeholder="请输入名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="手机号">
                <a-input allow-clear placeholder="请输入手机号" v-model="queryParam.phone" />
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearch">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
        <a-button type="primary" style="margin-bottom: 8px" icon="plus" @click="addStore">添加药房</a-button>
      </div>

      <a-table :scroll="{ x: true }" ref="table" @change="tablePaginationChange" :pagination="pagination" bordered
        :dataSource="dataSource" :columns="columns" :rowKey="(record,index)=> index">
        <img class="banner-img" slot-scope="text, record" :src="record.logo" slot="logo" />
        <div slot="area" slot-scope="text, record" href="javascript:;">
          {{record.province}} {{record.city}} {{record.county}}
        </div>
        <div slot="status" slot-scope="text, record" href="javascript:;">
          <a-switch @change="changeStatus($event,record)" v-model:checked="record.status==2?false:true" />
        </div>
        <div slot="action" slot-scope="text, record" href="javascript:;">
          <a-button size='small' type="link" @click="updateInfo(record)">
            修改
          </a-button>
          <a-divider type="vertical" />
          <a-button size='small' type="link" @click="showJixing(record)">
            剂型
          </a-button>
        </div>
      </a-table>
    </a-card>
    <addBox ref="addBox" :visible="addFlag" @cancel="addCancel" @ok="AddSuccess">
    </addBox>
    <!-- 剂型 -->
    <jxdBox ref="jx"></jxdBox>
  </page-header-wrapper>
</template>

<script>
  import {
    STable,
    Ellipsis,
  } from '@/components'
  import {
    pharmacysList,
    hospitalDelet,
    pharmacysStatus
  } from '@/api/pharmacys'
  import addBox from './add'
  import jxdBox from './jixing'

  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    createVNode
  } from 'vue';
  import {
    Modal
  } from 'ant-design-vue';


  export default {
    components: {
      STable,
      Ellipsis,
      addBox,
      jxdBox
    },
    data() {
      return {
        shenhe: 0, //审核
        status: 0, //状态
        addFlag: false,
        pagination: {
          current: 1, //默认当前叶数
          total: 0, // 数据总数 如果不设置就以绑定的数据量为准
          pageSize: 10, // 默认每页条数
          showSizeChanger: true, //是否可以改变 pageSize
          pageSizeOptions: ['10', '20', '40', '80', '100'], //可切换每叶数据条数
          showTotal: total => this.$t('共') + ' ' + `${total}` + ' ' + this.$t('条数据'), // 分页中显示总的数据
          hideOnSinglePage: false // 只有一页时是否隐藏分页器 true为不显示 false为显示
        },
        queryParam: {
          name: '',
          phone: ''
        },
        dataSource: [],
        columns: [{
            title: 'id',
            dataIndex: 'id',
            key: 'id',
            width: 100,
          },
          {
            title: 'logo',
            key: 'logo',
            dataIndex: 'logo',
            width: 100,
            scopedSlots: {
              customRender: 'logo'
            }
          },
          {
            title: '药房名称',
            dataIndex: 'name',
            key: 'name',
            ellipsis: true,
          },
          {
            title: '地址',
            dataIndex: 'area',
            key: 'area',
            ellipsis: true,
            scopedSlots: {
              customRender: 'area'
            }
          },
          {
            title: '手机号',
            dataIndex: 'phone',
            key: 'phone',
            ellipsis: true,
          },
          {
            title: '邮箱',
            dataIndex: 'email',
            key: 'email',
            ellipsis: true,
          },
          {
            title: '状态',
            dataIndex: 'status',
            key: 'status',
            scopedSlots: {
              customRender: 'status'
            }
          },
          {
            title: '操作',
            key: 'operation',
            fixed: 'right',
            width: 190,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        searchflag: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 可做剂型
      showJixing(v) {
        console.log('00000', this.$refs.jx)
        this.$refs.jx.initData(v)
      },
      changeStatus(e, v) {
        pharmacysStatus({
          id: v.id,
          status: e ? 1 : 2
        }).then(res => {
          this.$message.success('修改成功')
          this.getList()
        })
      },
      handleSearch() {
        this.pagination.current = 1
        this.searchflag = true
        this.getList()
      },
      resetSearch() {
        this.pagination.current = 1
        this.searchflag = false
        this.queryParam = {
          name: '',
          phone: ''
        }
        this.getList()
      },
      getList() {
        let data = {}
        if (this.searchflag) {
          data = {
            pageNo: this.pagination.current,
            pageSize: this.pagination.pageSize,
          }
          data = Object.assign({}, data, this.queryParam)
        } else {
          data = {
            pageNo: this.pagination.current,
            pageSize: this.pagination.pageSize,
          }
        }
        pharmacysList(data).then(res => {
          this.pagination.total = res.data.totalCount
          this.dataSource = res.data.data
        })
      },

      updateInfo(val) {
        this.addFlag = true
        this.$nextTick(() => {
          this.$refs.addBox.setUpdate(val)
        })
      },

      addCancel() {
        this.addFlag = false
      },
      AddSuccess() {
        this.addFlag = false
        this.getList()
      },
      addStore() {
        this.addFlag = true
        this.$nextTick(() => {
          this.$refs.addBox.updateflag = false
        })
      },
      tablePaginationChange(e) {
        this.pagination = e
        this.getList()
      },
      getDate(stamp) {
        var date = new Date(stamp * 1000)
        return date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? ('0' + (date.getMonth() + 1)) : date.getMonth() +
          1) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      },
      deletInfo(val) {
        hospitalDelet(val.id).then(res => {
          this.getList()
        })
      }
    }
  }
</script>
<style scoped>
  .banner-img {
    width: 50px;
    height: 50px;
    border-radius: 5px;
  }
</style>